<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\common\builder\ZBuilder;
use app\admin\model\Scenicspot as ScenicspotModel;
use app\admin\model\Adminperiphery as AdminperipheryModel;
use think\Hook;

/**
 * 系统模块控制器
 * @package app\admin\controller
 */
class Scenicspot extends Admin
{
    /**
     * 系统设置
     * @param string $group 分组
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     */
    public function index($id=0){
        // 保存数据
        // 查询
        $map = $this->getMap();
        if($id != 0){
            $map[] = ['id','=',$id ];
        }
        $order = $this->getOrder( 'solid asc,id desc');
        // 排序
        // 数据列表
        $map[] = ['del','=',0];
        $data_list =  ScenicspotModel::getAll($map,'*',$order);
        //$data_list = BannerModel::where($map)->order('id desc')->paginate();
        // 分页数据
        $page = $data_list->render();
        $btn_accessedit = [
            'title' => '编辑',
            'icon'  => 'fa fa-pencil',
            'href'  => url('edit', ['id' => '__id__','aaa'=>'1','type'=>'0'])
        ];

        $periphery = [
            'title' => '周边',
            'icon'  => 'fa fa-fw fa-building-o',
            'href'  => url('periphery', ['pid' => '__id__','aaa'=>'1','type'=>'0'])
        ];
        $btn_accessedl = [
            'title' => '删除',
            'icon'  => 'fa fa-times',
            'class'=> 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('edit', ['id' => '__id__','type'=>'2'])
        ];
        $btn_access = [
            'title' => '添加景区',
            'icon'  => 'fa fa-plus-circle',
            'class'=>"btn btn-primary",
            'href'  => url('edit')
        ];
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('首页数据') // 设置页面标题
            ->setSearchArea([
                ['select:2', 'flag', '审核状态','','',['审核中','审核成功','审核失败']],
                ['select:2', 'score_flag', '是否使用积分', '', '', ['送积分','不送积分']],
                ['text:2', 'title', '景区名称','like'],
                ['text:2', 'desc', '景区描述','like'],
                ['text:2', 'map_address', '景区地址','like'],
            ])
            ->hideCheckbox()
            ->addColumns([ // 批量添加数据列
                ['id', '编号'],
                ['title', '景区名称'],
                ['uname', '添加人'],
                ['pic','展示图','picture'],
                ['money','价格'],
                ['score_flag','是否送积分','status','',['不送积分','送积分']],
                ['map_address','详细地址'],
                ['add_time', '添加时间', ],
                ['com_num', '评论数' ],
                [ 'flag', '审核状态','status','',['审核中','审核成功','审核失败']],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', $btn_access,['area'=>['800px','80%']]) // 添加授权按钮
            ->addRightButton('editcode',$btn_accessedit,['area'=>['800px','80%']]) // 批量添加右侧按钮
            ->addRightButton('del',$btn_accessedl) // 批量添加右侧按钮
            ->addRightButton('periphery',$periphery) // 批量添加右侧按钮
            ->setRowList($data_list) // edit
            ->setPages($page) // 设置分页数据
            ->fetch(); // 渲染页面
    }

    public function edit($id = 0,$aaa = '1',$type=0)
    {

        if($type == '2'){
            ScenicspotModel::saveData(['del'=>1],['id'=>$id]);
            $this->success('操作成功');
        }

        // 保存数据
        if ($this->request->isPost()) {
            $data = input('post.');
            $data['type'] = implode(',',$data['type']);
            unset($data['__token__']);
            $xmlData = file_get_contents('https://restapi.amap.com/v3/geocode/regeo?output=xml&location='.$data['map'].'&key=f79f23aae7273821ad7b3a7d85fbc9d1');
            //将xml格式转换为数组
            $dizhidata = FromXml($xmlData);
            if ($dizhidata['info'] != 'OK') {
                return json(array('code'=>'1','msg'=>'参数错误！'));
            }
            $regeocode = $dizhidata['regeocode']['addressComponent'];
            $data['province'] = $regeocode['province'];
            $data['city'] = $regeocode['city'];
            $data['area'] = $regeocode['district'];
            $data['province_id'] = db('china')->where(['name'=>$data['province']])->value('id');
            $data['city_id'] = db('china')->where(['name'=>$data['city']])->value('id');
            $data['area_id'] =db('china')->where(['name'=>$data['area']])->value('id');
            $userinfo = session('user_auth');

            $data['uname'] =    $userinfo['nickname'];
            $data['umobile'] =    $userinfo['mobile'];
            $data['content'] =    $_POST['content'];
            $data['pic_url'] =    get_file_path($data['pic']);
            $topicstrarr = explode(',',$data['pic_top']);
            $toppicarr = array();
            foreach ($topicstrarr as $k=>$v){
                $toppicarr[] = get_file_path($v);
            }
            $data['pic_top_url'] =   implode(',',$toppicarr);
            $where = '';
            if($data['id'] != '' && $data['id'] !='0' && !empty($data['id'])){
                $where = 'id='.$data['id'];
            }
            $data['add_time'] =  date('Y-m-d H:i:s');
            $data['add_uid'] =  UID;

            ScenicspotModel::saveData($data,$where);
            $this->success('编辑成功','',['_close_pop' => 1,'_parent_reload' => 1]);
        }
        $scenic_spot_arr = config('scenic_spot_arr');
        // 获取数据
        $info = ScenicspotModel::getInfo('id='.$id,'*');
        // 使用ZBuilder快速创建表单
            return ZBuilder::make('form')
                ->setPageTitle('编辑') // 设置页面标题
                ->addFormItems([ // 批量添加表单项
                    ['hidden', 'id'],
                    ['static','add_time','添加时间'],
                    ['text','title', '景区名称'],
                    ['image','pic','展示图'],
                    ['images','pic_top','头部图片'],
                    ['text','money','价格'],
                    ['select','score_flag','是否送积分','',['不送积分','送积分']],
                    ['checkbox','type','景区类型','',$scenic_spot_arr],
                    ['radio', 'flag', '审核状态','',['审核中','审核成功','审核失败']],
                    ['text','solid','排序'],
                    ['textarea','desc','景区简介'],
                    ['ueditor','content','详情'],

                ])
                ->addBmap('map', '地图', 'k5X4mt6dDU3VDvgvu4MzkR6GOOESVvo0')
                ->setFormData($info) // 设置表单数据
                ->fetch();
    }

    //景区的 需要有 餐饮 住宿  和 宿营地  //图片 +
    public function periphery($pid){
        // 查询
        $map = $this->getMap();
        // 排序
        // 数据列表
        $order = $this->getOrder( 'solid asc,id desc');
        $map[] = ['del','=',0];
        $map[] = ['pid','=',$pid];

        $data_list =  AdminperipheryModel::getAll($map,'*',$order);
        $data_list->each(function ($data){
            $scenic_spot_arr = config('periphery');
            $sssss =  explode(',',$data['type']);
            foreach ($sssss as $k=>$v){
                $sssss[$k] = $scenic_spot_arr[$v];
            }

            $data['type'] = implode(',',$sssss);
            return $data;
        });
        //$data_list = BannerModel::where($map)->order('id desc')->paginate();
        // 分页数据
        $page = $data_list->render();
        $btn_accessedit = [
            'title' => '修改',
            'icon'  => 'fa fa-pencil',
            'href'  => url('peripheryedit', ['id' => '__id__','pid'=>$pid,'type'=>'0'])
        ];

        $btn_accessedl = [
            'title' => '删除',
            'icon'  => 'fa fa-times',
            'class'=> 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('peripheryedit', ['id' => '__id__','pid'=>$pid,'type'=>'2'])
        ];
        $btn_access = [
            'title' => '添加周边信息',
            'icon'  => 'fa fa-plus-circle',
            'class'=>"btn btn-primary",
            'href'  => url('peripheryedit',['id' => '0','pid'=>$pid,'type'=>'0'])
        ];
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('周边') // 设置页面标题
            ->setSearchArea([
                ['select:2', 'flag', '审核状态','','',['审核中','审核成功','审核失败']],
                ['select:2', 'score_flag', '是否使用积分', '', '', ['送积分','不送积分']],
                ['text:2', 'title', '周边名称','like'],
                ['text:2', 'desc', '周边描述','like'],
                ['text:2', 'map_address', '周边地址','like'],
            ])
            ->hideCheckbox()
            ->addColumns([ // 批量添加数据列
                ['id', '编号'],
                ['title', '名称'],
                ['uname', '添加人'],
                ['pic','展示图','picture'],
                ['money','价格'],
                ['type','类型'],
                ['score_flag','是否送积分','status','',['不送积分','送积分']],
                ['map_address','详细地址'],
                ['add_time', '添加时间', ],
                [ 'flag', '审核状态','status','',['审核中','审核成功','审核失败']],
                ['right_button', '操作', 'btn']
            ])

//            ->addTopButton('add',$btn_access,['area'=>['800px','80%']]) // 批量添加顶部按钮
            ->addRightButton('editcode',$btn_accessedit,['area'=>['800px','80%']]) // 批量添加右侧按钮

            ->addRightButton('del',$btn_accessedl) // 批量添加右侧按钮
            ->setRowList($data_list) // edit
            ->setPages($page) // 设置分页数据
            ->fetch(); // 渲染页面
    }

    public function peripheryedit($id=0,$pid,$type=1)
    {
        if($type == '2'){
            AdminperipheryModel::saveData(['del'=>1],['id'=>$id]);
            $this->success('操作成功');
        }

        // 保存数据
        if ($this->request->isPost()) {
            $data = input('post.');
            $data['type'] = implode(',',$data['type']);
            unset($data['__token__']);
            $xmlData = file_get_contents('https://restapi.amap.com/v3/geocode/regeo?output=xml&location='.$data['map'].'&key=f79f23aae7273821ad7b3a7d85fbc9d1');
            //将xml格式转换为数组
            $dizhidata = FromXml($xmlData);
            if ($dizhidata['info'] != 'OK') {
                return json(array('code'=>'1','msg'=>'参数错误！'));
            }
            $regeocode = $dizhidata['regeocode']['addressComponent'];
            $data['province'] = $regeocode['province'];
            $data['city'] = $regeocode['city'];
            $data['area'] = $regeocode['district'];
            $data['province_id'] = db('china')->where(['name'=>$data['province']])->value('id');
            $data['city_id'] = db('china')->where(['name'=>$data['city']])->value('id');
            $data['area_id'] =db('china')->where(['name'=>$data['area']])->value('id');
            $userinfo = session('user_auth');

            $data['pid'] = $pid;
            $data['uname'] =    $userinfo['nickname'];
            $data['umobile'] =    $userinfo['mobile'];
            $data['content'] =    $_POST['content'];
            $data['pic_url'] =    get_file_path($data['pic']);
            $topicstrarr = explode(',',$data['pic_top']);
            $toppicarr = array();
            foreach ($topicstrarr as $k=>$v){
                $toppicarr[] = get_file_path($v);
            }
            $data['pic_top_url'] =   implode(',',$toppicarr);
            $where = '';


             $where = 'id='.$data['id'];
             $periphery =  AdminperipheryModel::where(['id'=>$data['id']])->value('add_uid');
            $role = 6;
            if($data['flag'] == '1'){
                 //审核通过
                 if($data['type'] == '1' || $data['type'] == '2' || $data['type'] == '3'){
                     $role = 7;
                 }else{
                     $role = 8;
                 }
             }
            db('admin_user')->where('id>1 and id='.$periphery)->setField(['role'=>$role]);

            //如果状态变了的话  是需更改用户状态的


            AdminperipheryModel::saveData($data,$where);

            $this->success('编辑成功','',['_close_pop' => 1,'_parent_reload' => 1]);
        }
        $scenic_spot_arr = config('periphery');
        // 获取数据
        $info = AdminperipheryModel::getInfo('id='.$id,'*');
        // 使用ZBuilder快速创建表单

        return ZBuilder::make('form')
            ->setPageTitle('周边信息') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden', 'id'],
                ['hidden', 'pid'],
                ['static','add_time','添加时间'],
                ['text','title', '名称'],
                ['image','pic','展示图'],
                ['images','pic_top','头部图片'],
                ['text','money','价格'],
                ['text','phone','联系电话'],
                ['select','score_flag','是否送积分','',['不送积分','送积分']],
                ['checkbox','type','景区类型','',$scenic_spot_arr],
                ['radio','is_top','是否推荐展示在景区详情','',['否','是']],
                ['text','solid','排序'],
                ['radio', 'flag', '审核状态','',['审核中','审核成功','审核失败']],
                ['textarea','desc','周边简介'],
                ['ueditor','content','详情'],

            ])
            ->addBmap('map', '地图', 'k5X4mt6dDU3VDvgvu4MzkR6GOOESVvo0')
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }
}